using System;
using SharpObjects.Utils;

namespace SharpObjects.Cryptography
{
	public class KeyFile
	{
		protected string m_sFile;
		protected ICryptoProvider m_provider;

		public KeyFile(string sFile, ICryptoProvider provider)
		{
			m_sFile = sFile;
			m_provider = provider;
		}

		public ICryptoProvider Provider { get { return m_provider; } }

		public void Load()
		{
			ICryptoKey key = (ICryptoKey)FilePersistence.Load(m_sFile, null);
			
			if(key == null)
			{
				throw new Exception(string.Format("File does not exist: {0}", m_sFile));
			}

			m_provider.CryptoKey = key;
		}

		public void Save()
		{
			FilePersistence.Save(m_provider.CryptoKey, m_sFile);
		}
	}
}
